(function ($) {
    $.fn.yqTextVirtual = function (opts, param) {
        if (typeof opts == 'string') {
            return $.fn.yqTextVirtual.methods[opts](this, param);
        }

        return this.each(function () {
            $this = $(this);
            var opt;
            if ($this.data("yqtype") == "yqTextVirtual") {
                opt = $.extend({}, options($this), opts);
            }
            else {
                opt = $.extend({}, $.fn.yqTextVirtual.defaults, opts);
                var $uniqueID = $this.attr("ID");
                if ($uniqueID === undefined) {
                    $uniqueID = $.getUniqueID();
                    $this.attr("ID", $uniqueID);
                }
            }

            if (opt.width === undefined && $this.width() > 0) {
                opt.width = $this.width();
            }

            //变量信息
            $this.data("textvirtual_opt", opt);
            $this.data("yqtype", "yqTextVirtual");

            init_text($this);
        });
    };

    $.fn.yqTextVirtual.defaults = {
        ui_type: 15,
        ui_mask: {},
        param: {
            titles: [],
            model: []
        },
        isEdit: true,
        editModeW: 500, //模态编辑器宽度 格式为 500（数字）
        editModeH: 400, //模态编辑器高度 格式为 400（数字）
        beforeAdd: function (rows, row, callback) {
            callback(row);
        },
        beforeChange: function (rows, callback) {
            callback(true, rows);
        }
    };

    $.fn.yqTextVirtual.methods = {
        options: function (jq) {
            return options($(jq[0]));
        },
        destroy: function (jq) {
            destroy($(jq[0]));
        },
        resize: function (jq, width) {
            resize($(jq[0]), width);
        },
        clear: function (jq) {
            setRows($(jq[0]), {});
        },
        setValue: function (jq, value) {
            setValue($(jq[0]), value);
        },
        getValue: function (jq) {
            return getValue($(jq[0]));
        },
        isEdit: function (jq, tf) {
            isEdit($(jq[0]), tf);
        }
    };

    function options($object) {
        return $object.data("textvirtual_opt");
    }

    function destroy($object) {
        $object.textbox("destroy");
    }

    function resize($object, width) {
        $object.textbox("resize", width);
    }

    function init_text($object) {
        var opt = options($object);

        var text_opt = {
            editable: false,
            iconAlign: 'right',
            icons: [{
                iconCls: 'icon2-edit',
                handler: function (e) {
                    var pp = $object.textbox("textbox");
                    prevent_e = $.getPreventDefault(pp[0]);
                    prevent_e.id = $object.attr("id");
                    prevent_e.options = opt;

                    var mdiinfo = $.yqGetWindParams();

                    var run_opt = {
                        mdiinfo: mdiinfo,
                        prevent_e: prevent_e,
                        width: opt.editModeW,
                        height: opt.editModeH,
                        ui_type: opt.ui_type,
                        ui_mask: opt.ui_mask,
                        param: opt.param,
                        isEdit: opt.isEdit,
                        callback: function (value) {
                            //嵌入到datagrid中，兼容处理（需要重新创建对象）
                            if (typeof opt.p_target != "undefined") {
                                var ed = $.yqTextEditor(opt.p_target, opt.p_field);
                                if (!(ed === null)) {
                                    $(ed.target).yqTextVirtual('setValue', value);
                                }
                            }
                            else {
                                setValue($object, value);
                            }
                        }
                    };
                    if (window.top == window.self) {
                        $.yqVirtualPopEdit(run_opt);
                    }
                    else {
                        window.top.$.yqVirtualPopEdit(run_opt);
                    }
                }
            }, {
                iconCls: 'icon-clear',
                handler: function (e) {
                    setValue($object, {
                        titles: [],
                        model: []
                    });
                }
            }]
        };

        $object.textbox(text_opt);

        setText($object);
        isEdit($object, opt.isEdit);
    }

    function setValue($object, value) {
        var opt = options($object);
        if (typeof value === "string") {
            value = JSON.parse(value);
        }

        opt.ui_type = value.ui_type;
        opt.param = value.param;
        delete value.ui_type;
        delete value.param;
        opt.ui_mask = value;

        setText($object);
    }

    function getValue($object) {
        var opt = options($object);
        var ui_mask = $.extend({}, opt.ui_mask);
        ui_mask.ui_type = opt.ui_type;
        ui_mask.param = opt.param;
        return ui_mask;
    }

    function setText($object) {
        var opt = options($object);
        var ui_mask = $.extend({}, opt.ui_mask);
        ui_mask.ui_type = opt.ui_type;
        ui_mask.param = opt.param;
        //console.log("opt.titles",opt.titles);
        if (ui_mask.param.titles.length > 0) {
            $object.textbox("setText", JSON.stringify(ui_mask));
        }
        else {
            $object.textbox("setText", "");
        }
    }

    function isEdit($object, tf) {
        var opt = options($object);
        var icon0 = $object.textbox("getIcon", 0);
        var icon1 = $object.textbox("getIcon", 1);
        opt.isEdit = tf;
        if (opt.isEdit) {
            icon0.removeClass('icon-more');
            icon0.addClass('icon-edit');
            icon1.show();
        } else {
            icon0.removeClass('icon-edit');
            icon0.addClass('icon-more');
            icon1.hide();
        }
    }
})(jQuery);
